package org.jivesoftware.spark.crowd.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import org.jivesoftware.resource.SparkRes;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.Workspace;
import org.jivesoftware.spark.component.VerticalFlowLayout;
import org.jivesoftware.spark.component.panes.CollapsiblePane;
import org.jivesoftware.spark.component.panes.CollapsibleTitlePane;
import org.jivesoftware.spark.crowd.CrowdsManager;
import org.jivesoftware.spark.crowd.Param;
/**
 * 
 * @author liyuan
 *
 */
public class CrowdList extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 13579L;
	/**
	 * show all {@link CrowdItem} on local client 
	 */
	private JPanel crowds;
	/**
	 * contains crowds panel
	 */
	private CollapsiblePane root;
	private JPanel middlePanel;
	private JScrollPane scrollPane;
	private static CrowdList basePanel;
	
	private JPopupMenu pop;
	
	private UIEventManager uiEventManager;
	
	private static final Object LOCK = new Object();
	/**
	 * assemble panels,sorted from "top" to "bottom" :crowds,root,middlePanel,scrollPane,basePanel(this)
	 */
	private CrowdList(){
		uiEventManager = CrowdsManager.getInstance().getUIEventManager();
		
		crowds = new JPanel(new VerticalFlowLayout());
		crowds.setBackground((Color)UIManager.get("List.background"));
		
		root = new CollapsiblePane();
		root.setTitle("我加入的群");
		root.setContentPane(crowds);
		
		middlePanel = new JPanel(new VerticalFlowLayout(VerticalFlowLayout.TOP, 0, 0, true, false));
		middlePanel.setBackground((Color)UIManager.get("List.background"));
		middlePanel.add(root,0);
		
		scrollPane = new JScrollPane(middlePanel);
		scrollPane.setBorder(new EmptyBorder(0,0,0,0));
		
		
		setLayout(new BorderLayout());
		add(scrollPane,"Center");
		
		initialize();
		
	}
	
	public static CrowdList getInstance(){
		synchronized(LOCK){
		if(basePanel == null)
			basePanel = new CrowdList();
		}
		return basePanel;
	}
	/**
	 * add listeners to this panel,then add this to {@link Workspace}
	 * you can also extends this class but don't invoke this method and add yourselves function
	 */
	private void initialize(){
		
		pop = new JPopupMenu();
		JMenuItem createItem = new JMenuItem(Param.getLabelText("createCrowd", "创建一个新群"));
		createItem.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				CreationDialog dlg = new CreationDialog();
				dlg.showDialog(null);
				
			}
			
		});
		JMenuItem findItem = new JMenuItem(Param.getLabelText("searchCrowd", "查找群"));
		findItem.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				SearchDialog sd = new SearchDialog();
				sd.showDialog(null);
			}
			
		});
		pop.add(createItem);
		pop.add(findItem);
		root.getTitlePane().addMouseListener(new MouseAdapter(){

			@Override
			public void mousePressed(MouseEvent e) {
				
				if(e.getButton() == 3){
					pop.show(e.getComponent(), e.getX(), e.getY());
				}
				
			}
			
		});
		SparkManager.getWorkspace().getWorkspacePane().addTab("群", SparkRes.getImageIcon(SparkRes.SMALL_ALL_CHATS_IMAGE), this);
	
	}
	/**
	 * add a {@link CrowdItem} to the panel at a special position.
	 * @param item 
	 * @param index 
	 * @return {@link CrowdItem} be added
	 */
	public CrowdItem addCrowd(CrowdItem item,int index){
		crowds.add(item, index);
		crowds.validate();crowds.repaint();
		return item;
	}
	/**
	 * add a {@link CrowdItem} to the panel at the last position.
	 * @param item
	 * @return
	 */
	public CrowdItem addCrowd(CrowdItem item){	
		return addCrowd(item, crowds.getComponentCount());
	}
	/**
	 * remove a crowd label from panel
	 * @param crowdName
	 */
	public void removeCrowd(String crowdName) {
		Component[] cs = crowds.getComponents();
		for(Component c : cs){
			CollapsibleTitlePane crowd = (CollapsibleTitlePane)c;
			if(crowd.getText().equals(crowdName))
				crowds.remove(crowd);
		}
		
		crowds.validate();
		crowds.repaint();
			
		
		
	}
	
}
